package com.pf.helper;

import cn.hutool.core.util.IdUtil;
import com.pf.dto.FileDTO;
import com.pf.dto.UploadFileResp;
import com.pf.exception.BusinessException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/7/1 22:25
 */
@Component
public class CustomFileHelper {

    /**
     * 临时文件存储路径前缀
     */
    @Value("${file.upload.temp.base.path:/data/file/upload/temp}")
    private String tempFilePathPrefix;

    /**
     * 确认文件存储路径前缀
     */
    @Value("${file.upload.temp.base.path:/data/file/upload/confirm}")
    private String confirmFilePathPrefix;

    @Value("${file.download.url.prefix:http://localhost:8080/file/download/}")
    private String fileDownloadUrlPrefix;

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    public UploadFileResp upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = IdUtil.createSnowflake(1, 1).nextIdStr();
        // 创建临时目录
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String tempFiledDirPathStr = tempFilePathPrefix + "\\" + sdf.format(new Date());
        File tempFiledDir = new File(tempFilePathPrefix);
        if (!tempFiledDir.exists()) {
            tempFiledDir.mkdirs();
        }
        String tempFilePath = tempFilePathPrefix + "\\" + fileId + fileSuffix;
        File tempFile = new File(tempFilePath);
        FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);

        UploadFileResp fileDTO = UploadFileResp.builder()
                .fileId(fileId)
                .fileName(originalFilename)
                .build();
//        file.transferTo(tempFilePath);
        return fileDTO;
    }

    /**
     * 确认文件上传
     * <p>
     * 逻辑：将上传的文件从临时目录移到确认目录
     *
     * @param fileId
     * @param fileName
     * @return
     */
    public FileDTO confirmUploadFile(String fileId, String fileName) throws Exception {
        String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
        String tempFilePath = tempFilePathPrefix + "\\" + fileId + fileSuffix;
        File tempFile = new File(tempFilePath);
        if (!tempFile.exists()) {
            throw new BusinessException("文件不存在！fileId=" + fileId);
        }
        File confirmFileDir = new File(confirmFilePathPrefix);
//        if (!confirmFileDir.exists()) {
//            confirmFileDir.mkdirs();
//        }
//        String confirmFilePath = confirmFilePathPrefix + "\\" + fileId + fileSuffix;
//        File confirmFile = new File(confirmFilePath);
        FileUtils.moveToDirectory(tempFile, confirmFileDir, true);
        return FileDTO.builder()
                .fileId(fileId)
                .fileName(fileName)
                .fileSize(tempFile.length())
                .downloadUrl(fileDownloadUrlPrefix + fileId)
                .build();
    }

    public File getFile(String fileId, String fileSuffix) {
        String filePath = confirmFilePathPrefix + "\\" + fileId + fileSuffix;
        return new File(filePath);
    }
}
